package cn.iocoder.yudao.module.project.service.kismaterielexwarehousehistory;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.kismaterielexwarehousehistory.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.kismaterielexwarehousehistory.KisMaterielExWarehouseHistoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.kismaterielexwarehousehistory.KisMaterielExWarehouseHistoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 物料出库单历史数据 Service 实现类
 *
 * @author 武汉华诚
 */
@Service
@Validated
public class KisMaterielExWarehouseHistoryServiceImpl implements KisMaterielExWarehouseHistoryService {

    @Resource
    private KisMaterielExWarehouseHistoryMapper kisMaterielExWarehouseHistoryMapper;

    @Override
    public String createKisMaterielExWarehouseHistory(KisMaterielExWarehouseHistorySaveReqVO createReqVO) {
        // 插入
        KisMaterielExWarehouseHistoryDO kisMaterielExWarehouseHistory = BeanUtils.toBean(createReqVO, KisMaterielExWarehouseHistoryDO.class);
        kisMaterielExWarehouseHistoryMapper.insert(kisMaterielExWarehouseHistory);
        // 返回
        return kisMaterielExWarehouseHistory.getId();
    }

    @Override
    public void updateKisMaterielExWarehouseHistory(KisMaterielExWarehouseHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateKisMaterielExWarehouseHistoryExists(updateReqVO.getId());
        // 更新
        KisMaterielExWarehouseHistoryDO updateObj = BeanUtils.toBean(updateReqVO, KisMaterielExWarehouseHistoryDO.class);
        kisMaterielExWarehouseHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteKisMaterielExWarehouseHistory(String id) {
        // 校验存在
        validateKisMaterielExWarehouseHistoryExists(id);
        // 删除
        kisMaterielExWarehouseHistoryMapper.deleteById(id);
    }

    private void validateKisMaterielExWarehouseHistoryExists(String id) {
        if (kisMaterielExWarehouseHistoryMapper.selectById(id) == null) {
            throw exception(KIS_MATERIEL_EX_WAREHOUSE_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public KisMaterielExWarehouseHistoryDO getKisMaterielExWarehouseHistory(String id) {
        return kisMaterielExWarehouseHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<KisMaterielExWarehouseHistoryDO> getKisMaterielExWarehouseHistoryPage(KisMaterielExWarehouseHistoryPageReqVO pageReqVO) {
        return kisMaterielExWarehouseHistoryMapper.selectPage(pageReqVO);
    }

}